﻿using System;
using System.Collections.Generic;
using System.Globalization;
using System.Linq;
using System.Reflection;
using System.Text;
using System.Text.RegularExpressions;
using System.Web;
using System.Web.Security;

namespace Sys.Common
{
    /// <summary>
    /// 字符串处理
    /// </summary>
    public static class StringHelper
    {
        /// <summary>
        /// 获取Guid并且去除"-"符号
        /// </summary>
        /// <returns>精简后的Guid字符串</returns>
        public static string GetGuid()
        {
            return Guid.NewGuid().ToString().Replace("-", string.Empty);
        }

        /// <summary>
        /// 获取加密后的密码
        /// </summary>
        /// <param name="input">加密前的密码</param>
        /// <returns>加密后的密码</returns>
        public static string GetPassWord(string input)
        {
            return FormsAuthentication.HashPasswordForStoringInConfigFile(input, "MD5");
        }

        /// <summary>
        /// 截取指定长度文本
        /// </summary>
        /// <param name="value">原文本</param>
        /// <param name="length">指定长度</param>
        /// <returns>截取后文本</returns>
        public static string CutString(string value, int length)
        {
            Regex regex = new Regex("[\u4e00-\u9fa5]+", RegexOptions.Compiled);

            StringBuilder sb = new StringBuilder();
            char[] stringChar = value.ToCharArray();

            int nLength = 0; bool isCut = false;

            for (int i = 0; i < stringChar.Length; i++)
            {
                if (regex.IsMatch((stringChar[i]).ToString()))
                {
                    sb.Append(stringChar[i]); nLength += 2;
                }
                else
                {
                    sb.Append(stringChar[i]); nLength += 1;
                }

                if (nLength > length) { isCut = true; break; }
            }

            if (isCut) { return sb.ToString() + "..."; } else { return sb.ToString(); }
        }

        /// <summary>
        /// 获取字段内容拼接
        /// </summary>
        /// <typeparam name="T">实体类型</typeparam>
        /// <param name="list">实体列表</param>
        /// <param name="field">字段名称</param>
        /// <returns>拼接结果(分隔符",")</returns>
        public static string JoinField<T>(List<T> list, string field)
        {
            List<string> temp = new List<string>();

            foreach (var item in list)
            {
                PropertyInfo pi = typeof(T).GetProperty(field);

                temp.Add(pi.GetValue(item, null).ToString());
            }

            return string.Join(",", temp);
        }

        /// <summary>
        /// 删除所有HTML标签
        /// </summary>
        /// <param name="html">HTML</param>
        /// <returns>清理后的字符串</returns>
        public static string NoHTML(string html)
        {
            //删除脚本  
            html = Regex.Replace(html, @"<script[^>]*?>.*?</script>", "", RegexOptions.IgnoreCase);

            //删除HTML  
            html = Regex.Replace(html, @"<(.[^>]*)>", "", RegexOptions.IgnoreCase);
            html = Regex.Replace(html, @"([\r\n])[\s]+", "", RegexOptions.IgnoreCase);
            html = Regex.Replace(html, @"-->", "", RegexOptions.IgnoreCase);
            html = Regex.Replace(html, @"<!--.*", "", RegexOptions.IgnoreCase);

            html = Regex.Replace(html, @"&(quot|#34);", "\"", RegexOptions.IgnoreCase);
            html = Regex.Replace(html, @"&(amp|#38);", "&", RegexOptions.IgnoreCase);
            html = Regex.Replace(html, @"&(lt|#60);", "<", RegexOptions.IgnoreCase);
            html = Regex.Replace(html, @"&(gt|#62);", ">", RegexOptions.IgnoreCase);
            html = Regex.Replace(html, @"&(nbsp|#160);", " ", RegexOptions.IgnoreCase);
            html = Regex.Replace(html, @"&(iexcl|#161);", "\xa1", RegexOptions.IgnoreCase);
            html = Regex.Replace(html, @"&(cent|#162);", "\xa2", RegexOptions.IgnoreCase);
            html = Regex.Replace(html, @"&(pound|#163);", "\xa3", RegexOptions.IgnoreCase);
            html = Regex.Replace(html, @"&(copy|#169);", "\xa9", RegexOptions.IgnoreCase);
            html = Regex.Replace(html, @"&#(\d+);", "", RegexOptions.IgnoreCase);

            html.Replace("<", "");
            html.Replace(">", "");
            html.Replace("\r\n", "");
            html = HttpContext.Current.Server.HtmlEncode(html).Trim();

            return html;
        }

        /// <summary>
        /// 获取当前时间和星期几
        /// </summary>
        /// <returns>内容字符串</returns>
        public static string TimeAndDayOfWeek()
        {
            string time = DateTime.Now.ToString("yyyy年MM月dd日");
            return time + " " + CultureInfo.CurrentCulture.DateTimeFormat.GetDayName(DateTime.Now.DayOfWeek);
        }

        /// <summary>
        /// UCS2解码 
        /// </summary>
        /// <param name="src">UCS2 源串</param>
        /// <returns>解码后的UTF-16BE字符串</returns>
        public static string DecodeUCS2(string src)
        {
            string decucs = src.Remove(src.IndexOf("\r"));
            string pstr = "^[0-9a-fA-F]+$";

            if (!Regex.IsMatch(decucs, pstr))
            {
                return "非法字符串无法解析！";
            }

            StringBuilder builer = new StringBuilder();

            for (int i = 0; i < decucs.Length; i += 4)
            {
                int unicode_nu = Int32.Parse(decucs.Substring(i, 4), System.Globalization.NumberStyles.HexNumber);
                builer.Append(string.Format("{0}", (char)unicode_nu));
            }

            return builer.ToString();
        }

        /// <summary>
        /// UCS2编码
        /// </summary>
        /// <param name="src">UTF-16BE编码的源串</param>
        /// <returns>编码后的UCS2串</returns>
        public static string EncodeUCS2(string src)
        {
            StringBuilder builer = new StringBuilder();

            builer.Append("000800");
            byte[] tmpSmsText = Encoding.Unicode.GetBytes(src);
            builer.Append(tmpSmsText.Length.ToString("X2")); //正文内容长度
            for (int i = 0; i < tmpSmsText.Length; i += 2) //高低字节对调 
            {
                builer.Append(tmpSmsText[i + 1].ToString("X2"));//("X2")转为16进制
                builer.Append(tmpSmsText[i].ToString("X2"));
            }
            builer = builer.Remove(0, 8);

            return builer.ToString();
        } 
    }
}
